package gunfire.cards;

import basemod.abstracts.CustomCard;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DamageAllEnemiesAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.actions.watcher.ChooseOneAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import gunfire.GunfireMod;
import gunfire.characters.Taizi;

import java.util.ArrayList;

import static gunfire.GunfireMod.makeCardPath;

public class Sunder extends CustomCard {

    public static final String ID = GunfireMod.makeID(Sunder.class.getSimpleName());
    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG = makeCardPath("Sunder.png");
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardType TYPE = CardType.ATTACK;
    private static final CardTarget TARGET = CardTarget.ALL;
    public static final CardColor COLOR = Taizi.Enums.COLOR_GRAY;
    private static final int COST = 2;

    public Sunder() {
        super(ID, NAME, IMG, COST, DESCRIPTION, TYPE, COLOR, RARITY, TARGET);
        this.baseDamage = 14;
        this.isMultiDamage = true;
        this.baseBlock = 14;
    }

    @Override
    public void upgrade() {
        if (!this.upgraded) {
            this.upgradeName();
            this.upgradeDamage(6);
            this.upgradeBlock(6);
        }
    }

    private boolean isMidInHand(){
        int handSize = AbstractDungeon.player.hand.group.size();
        int handIndex = AbstractDungeon.player.hand.group.indexOf(this);
        return handSize % 2 == 1 && handIndex == handSize / 2;
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        if(this.isMidInHand()){
            this.addToBot(new GainBlockAction(p, p, this.block));
            this.addToBot(new DamageAllEnemiesAction(p, this.multiDamage, DamageInfo.DamageType.NORMAL, AbstractGameAction.AttackEffect.BLUNT_LIGHT));
        } else {
            ArrayList<AbstractCard> stanceChoices = new ArrayList<>();
            CustomCard c1 = new Sunder1();
            CustomCard c2 = new Sunder2();
            if(this.upgraded){
                c1.upgrade();
                c2.upgrade();
            }
            c1.applyPowers();
            c2.calculateCardDamage(null);
            stanceChoices.add(c1);
            stanceChoices.add(c2);
            this.addToBot(new ChooseOneAction(stanceChoices));
        }
    }

    @Override
    public void triggerOnGlowCheck() {
        if (this.isMidInHand()) {
            this.glowColor = AbstractCard.GOLD_BORDER_GLOW_COLOR.cpy();
        } else {
            this.glowColor = AbstractCard.BLUE_BORDER_GLOW_COLOR.cpy();
        }
    }

    @Override
    public AbstractCard makeCopy() {
        return new Sunder();
    }
}
